/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jcphotorename_1_0.v.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import jcphotorename_1_0.util.FileUtil;
import jcphotorename_1_0.util.ICallBack;
import sun.rmi.runtime.Log;

/**
 *
 * @author SoChishun
 * @version 1.0 2016-6-7
 */
public class FrmMain extends javax.swing.JFrame {

    /**
     * Creates new form FrmMain
     * @since 1.0 2016-6-7
     */
    public FrmMain() {
        initComponents();
        this.setTitle("照片重命名 1.0");
        btn_open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc = new JFileChooser();
                jfc.setDialogTitle("选择照片源目录");
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // 只能选择目录
                if (JFileChooser.APPROVE_OPTION == jfc.showOpenDialog(null)) {
                    File file = jfc.getSelectedFile();
                    tb_path.setText(file.getPath());
                    FileUtil.countFile(file, chk_digui.isSelected());
                    FileUtil.dirsizemb = Math.ceil(FileUtil.dirsize / 1024 / 1024);
                    lbl_status.setText("0 / " + FileUtil.n_file + " (共 " + FileUtil.n_dir + "个目录, " + FileUtil.dirsizemb + "MB)");
                }
            }
        });
        btn_open_dest.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc = new JFileChooser();
                jfc.setDialogTitle("选择照片输出目录");
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // 只能选择目录
                if (JFileChooser.APPROVE_OPTION == jfc.showOpenDialog(null)) {
                    File file = jfc.getSelectedFile();
                    tb_path_dest.setText(file.getPath());
                }
            }
        });
        btn_run.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File file = new File(tb_path.getText());
                File file2 = new File(tb_path_dest.getText());
                if (!file.isDirectory() || !file2.isDirectory()) {
                    JOptionPane.showMessageDialog(null, "目录无效!");
                    return;
                }
                pgb_main.setMaximum(FileUtil.n_file);
                FileUtil.renameFile(file, tb_path_dest.getText(), chk_digui.isSelected(), chk_keep.isSelected(), tb_rule.getText(), new ICallBack() {
                    @Override
                    public void execute() {
                        pgb_main.setValue(FileUtil.n_do);
                        pgb_main.repaint();
                        lbl_status.setText(FileUtil.n_do + " / " + FileUtil.n_file + " (共 " + FileUtil.n_dir + "个目录, " + FileUtil.dirsizemb + "MB)");
                        lbl_status.repaint();
                    }
                });
                JOptionPane.showMessageDialog(null, "处理完成!");
            }
        });

    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tb_path = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btn_open = new javax.swing.JButton();
        btn_run = new javax.swing.JButton();
        tb_rule = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        pgb_main = new javax.swing.JProgressBar();
        lbl_status = new javax.swing.JLabel();
        chk_digui = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        tb_path_dest = new javax.swing.JTextField();
        btn_open_dest = new javax.swing.JButton();
        chk_keep = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("选择照片目录：");

        btn_open.setMnemonic('O');
        btn_open.setText("浏览(O)");

        btn_run.setMnemonic('R');
        btn_run.setText("执行(R)");

        tb_rule.setText("'IMG'_yyyyMMdd_hhmmss");

        jLabel2.setText("名称规则：");

        lbl_status.setText("(0/0)");

        chk_digui.setText("递归子目录");

        jLabel3.setText("选择输出目录：");

        btn_open_dest.setMnemonic('D');
        btn_open_dest.setText("浏览(D)");

        chk_keep.setSelected(true);
        chk_keep.setText("保留源文件");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pgb_main, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tb_path, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tb_rule, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(chk_digui)
                                .addGap(14, 14, 14)
                                .addComponent(chk_keep)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_open))
                    .addComponent(lbl_status)
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tb_path_dest, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_open_dest)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btn_run)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tb_path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_open))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tb_rule, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(chk_digui)
                    .addComponent(chk_keep))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tb_path_dest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_open_dest))
                .addGap(18, 18, 18)
                .addComponent(btn_run, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lbl_status)
                .addGap(4, 4, 4)
                .addComponent(pgb_main, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmMain().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_open;
    private javax.swing.JButton btn_open_dest;
    private javax.swing.JButton btn_run;
    private javax.swing.JCheckBox chk_digui;
    private javax.swing.JCheckBox chk_keep;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lbl_status;
    private javax.swing.JProgressBar pgb_main;
    private javax.swing.JTextField tb_path;
    private javax.swing.JTextField tb_path_dest;
    private javax.swing.JTextField tb_rule;
    // End of variables declaration//GEN-END:variables
}
